{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Grove Light Sensor example\n",
    "----\n",
    "* [Introduction](#Introduction)\n",
    "* [Set up the board](#Set-up-the-board)\n",
    "* [Setup and read from the sensor](#Setup-and-read-from-the-sensor)\n",
    "* [Display a graph](#Display-a-graph)\n",
    "\n",
    "----\n",
    "## Introduction\n",
    "\n",
    "This example shows how to use the [Grove Light Sensor v1.1](http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor), and how to plot a graph using matplotlib.\n",
    "\n",
    "The Grove Light Sensor produces an analog signal which requires an ADC.\n",
    "\n",
    "The Grove Light Sensor, Pynq Grove Adapter, and Grove ADC are used for this example. \n",
    "\n",
    "The Grove ADC is an example of an I2C peripheral.\n",
    "\n",
    "\n",
    "When the ambient light intensity increases, the resistance of the LDR or Photoresistor will decrease. This means that the output signal from this module will be HIGH in bright light, and LOW in the dark. Values for the sensor ranges from ~5.0 (bright) to >100.0 (dark). The sensor is more sensitive to darkness, and saturates in ambient light. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----\n",
    "## Set up the board\n",
    "\n",
    "Start by loading the Base Overlay."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pynq.overlays.base import BaseOverlay\n",
    "base = BaseOverlay(\"base.bit\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Connect the peripheral to the board.\n",
    "1. Connect the Pynq Grove Adapter to ***PMODB.*** \n",
    "2. Connect ***Grove ADC*** port ***J1*** (SCL, SDA, VCC, GND) to port ***G4*** of the Pynq Grove Adapter. \n",
    "3. Connect the ***Grove ALS*** to port ***J2*** of the ***Grove ADC*** (GND, VCC, NC, SIG)\n",
    "\n",
    "----\n",
    "\n",
    "## Setup and read from the sensor\n",
    "Grove ADC provides a raw sample which is converted into resistance first and then converted into temperature.\n",
    "This example shows on how to read from the Grove Light sensor which is connected to the ADC."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pynq.lib.pmod import Grove_Light\n",
    "from pynq.lib.pmod import PMOD_GROVE_G4 # Import constants\n",
    "\n",
    "# Grove2pmod is connected to PMODB (2)\n",
    "# Grove ADC is connected to G4 (pins [6,2])\n",
    "lgt = Grove_Light(base.PMODB, PMOD_GROVE_G4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sensor_val = lgt.read()\n",
    "print(sensor_val)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The peripheral can be connected to other ports by specifying the interface and the port when the sensor is declared above. \n",
    "\n",
    "### Start logging once every 100ms for 10 seconds\n",
    "\n",
    "Executing the next cell will start logging the sensor values every 100ms, and will run for 10s. You can try covering and uncovering the light sensor to vary the measured signal.\n",
    "\n",
    "You can vary the logging interval and the duration by changing the values 100 and 10 in the cell below. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "lgt.set_log_interval_ms(100)\n",
    "lgt.start_log()\n",
    "time.sleep(10) # Change input during this time\n",
    "r_log = lgt.get_log()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---- \n",
    "## Display a graph\n",
    "\n",
    "This example reads multiple values over a 10 second period. \n",
    "\n",
    "To change the light intensity, cover and uncover the light sensor. In typical ambient light, there is no need to provide an external light source, as the sensor is already reading at full scale. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.plot(range(len(r_log)), r_log, 'ro')\n",
    "plt.title('Grove Light Sensor Plot')\n",
    "min_r_log = min(r_log)\n",
    "max_r_log = max(r_log)\n",
    "plt.axis([0, len(r_log), min_r_log, max_r_log])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
